package com.zjitc.pigspring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zjitc.pigspring.bean.po.Address;
import com.zjitc.pigspring.bean.po.Goods;
import com.zjitc.pigspring.bean.po.Order;
import com.zjitc.pigspring.bean.po.OrderVO;
import com.zjitc.pigspring.common.Codes;
import com.zjitc.pigspring.common.RedisSetNull;
import com.zjitc.pigspring.common.Result;
import com.zjitc.pigspring.service.AddressService;
import com.zjitc.pigspring.service.GoodsService;
import com.zjitc.pigspring.service.OrderService;
import com.zjitc.pigspring.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/getAll")
    public Result<List<OrderVO>> getAll(){
        Object list = redisUtil.get("orderList");
        if(list!=null){
            return Result.success((List<OrderVO>) list);
        }
        List<OrderVO> orders = orderService.getAll();
        if(orders!=null){

           redisUtil.set("orderList",orders);
            return Result.success(orders);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }


    @GetMapping("/selectBySeller/{id}")
    public Result<List<OrderVO>> selectBySeller(@PathVariable Integer id){
       List<OrderVO> list = orderService.getBySeller(id);
       if (list!=null){
           return Result.success(list);
       }else{
           return Result.error(Codes.GET_INFO_FAILED);
       }
    }


    @RedisSetNull
    @PostMapping("/addOne")
    public Result addOrder(@RequestBody Order order){
        System.out.println("=======================================");
        System.out.println(order.toString());

        boolean save = orderService.save(order);
        if(save){
            return Result.success(Codes.ADD_YES);
        }else{
            return Result.error(Codes.ADD_NO);
        }
    }

    @RedisSetNull
    @DeleteMapping("/deleteOne/{id}")
    public Result deleteOrderById(@PathVariable String id){
        boolean flag = orderService.removeById(id);
        if (flag){
            return Result.success(Codes.DELETE_YES);
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    @RedisSetNull
    @PostMapping("/deleteAll")
    public Result deleteOrder(@RequestBody List<String> ids){
        boolean flag = orderService.removeBatchByIds(ids);
        if (flag){
            return Result.success(Codes.DELETE_YES);
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }


    @GetMapping("/selectOne/{id}")
    public Result<Order> selectOneOrder(@PathVariable String id){
        Order order = orderService.getById(id);
        if(order==null){
            return Result.error(Codes.GET_INFO_FAILED);
        }else{
            return Result.success(order);
        }
    }

    @RedisSetNull
    @Transactional
    @PutMapping("/updateOne")
    public Result updateOrder(@RequestBody Order order){

        boolean flag1 = true;
        if(Codes.TWO.equals(order.getStatus())){
            Goods goods = goodsService.getById(order.getGoodId());
            goods.setCount(order.getCount()+goods.getCount());
            flag1 = goodsService.updateById(goods);
        }
        boolean flag = orderService.updateById(order);
        if (flag && flag1){
            return Result.success(Codes.UPDATE_YES);
        }else{
            return Result.error(Codes.UPDATE_NO);
        }
    }

    @PostMapping("/conditionQuery")
    public Result<List<OrderVO>> queryOrder(@RequestBody OrderVO orderVO){
        List<OrderVO> orders = orderService.getAllByGoodName(orderVO);
        if(orders!=null){
            return Result.success(orders);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @PostMapping("/conditionQueryBySeller")
    public Result<List<OrderVO>> queryOrderBySeller(@RequestBody OrderVO orderVO){
        List<OrderVO> orders = orderService.queryBySeller(orderVO);
        if(orders!=null){
            return Result.success(orders);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @GetMapping("/getByUser/{id}")
    public Result<List<OrderVO>> getCollectByUser(@PathVariable Integer id){
        List<OrderVO> list = orderService.getByUser(id);
        if (list==null){
            return Result.error(Codes.GET_INFO_FAILED);
        }else{
            return Result.success(list);
        }
    }
}
